import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.zs.Student;
import org.zs.javaConfig08.JavaConfig;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@SpringJUnitConfig(value = JavaConfig.class)
public class AppTest08 {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    public void test() {
        Integer count = jdbcTemplate.queryForObject("select count(*) from user", Integer.class);
        System.out.println(count);
    }

    @Test
    public void updateTest1() {
        String sql = "update user set username = ? where id = ?";
        int rows = jdbcTemplate.update(sql, "zs", 1);
        System.out.println(rows);
    }

    @Test
    public void queryOneTest2() {
        String sql = "select * from student  where id = ?";
        Student rows = jdbcTemplate.queryForObject(sql, new RowMapper<Student>() {
            @Override
            public Student mapRow(ResultSet rs, int rowNum) throws SQLException {

                Student student = new Student();
                student.setId(rs.getInt("id"));
                student.setName(rs.getString("name"));

                return student;
            }
        }, 3);
        System.out.println(rows);
    }

    @Test
    public void queryALLTest3() {
        String sql = "select * from student";
        List<Student> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
        System.out.println( query);


    }
}
